package com.app.service.teacher.impl;

import com.app.mapper.Information.GradeMapper;
import com.app.mapper.teacher.management.TeacherMapper;
import com.app.pojo.Grade;
import com.app.pojo.Teacher;
import com.app.service.teacher.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    TeacherMapper teacherMapper;

    @Autowired
    GradeMapper gradeMapper;

    @Override
    public Teacher getTeacherById(String id) {
        return teacherMapper.getTeacherById(id);
    }

    @Override
    public String updateInfo(Integer id, String name, Integer sex, String mobilePhone) {
        if(id==null || name==null || name=="" ||
                sex==null || mobilePhone==null || mobilePhone==""){
            return "用值为空";
        }
        Integer rows =teacherMapper.updateInfo(id,name,sex,mobilePhone);
        if(rows!=1){
            return "修改时未知异常";
        }
        return "修改成功";
    }

    @Override
    public String updatePassword(Integer id, String oldPassword ,String newPassword) {
        Teacher teacher = teacherMapper.getTeacherById2(id);
        if(!teacher.getPassword().equals(oldPassword)) {
            return "旧密码不正确";
        }
        Integer rows = teacherMapper.updatePassword(id,newPassword);
        if(rows!=1){
            return "修改密码未知异常";
        }
        return "密码修改成功,请重新登录";
    }

    @Override
    public List<Teacher> getTeachers() {
        return teacherMapper.getTeachers();
    }

    @Override
    public Integer updateStudentGrade(Grade grade,String classs) {
        Integer row = 0;
        grade.setTotal(grade.getIq()+grade.getCq()+
                grade.getPmq()+grade.getHq()+grade.getPq());
        gradeMapper.updateStudentGrade(grade);
        List<Grade> grades = gradeMapper.sortGrade(classs);
        Integer sort=1;
        for(Grade g:grades){
            g.setRanking(sort);
            row += gradeMapper.updateStudentGrade(g);
            sort++;
        }
        return row;
    }

    @Override
    public List<Teacher> getLikeTeacher(String name, Integer sex, String college) {
        Teacher teacher = new Teacher();
        teacher.setName(name);
        teacher.setSex(sex);
        teacher.setCollege(college);
        return teacherMapper.getLikeTeacher(teacher);
    }

    @Override
    public Integer delTeacher(Integer id) {
        return teacherMapper.delTeacher(id);
    }

    @Override
    public Integer addTeacher(Teacher teacher) {
        teacher.setPassword(teacher.getTeacherId());
        return teacherMapper.addTeacher(teacher);
    }

    @Override
    public Integer updateAll(Teacher teacher) {
        return teacherMapper.updateAll(teacher);
    }

    @Override
    public Grade getAVGGradeByCollege(String college) {
        return gradeMapper.getAVGGradeByCollege(college);
    }
}
